﻿using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using shenghua.Common.CustomValueType;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Marketing.SalesPromotions
{
    /// <summary>
    /// 促销活动
    /// </summary>
    public class SalesPromotion: Entity, ICreationAudited
    {
        public string Name { get; set; }

        public DateTime BeginTime { get; set; }

        public DateTime EndTime { get; set; }

        /// <summary>
        /// 活动价
        /// </summary>
        public Money Price { get; set; }

        public ValueArray<int> AppliedCommodityIds { get; set; }

        public SalesPromotionTargetType TargetType { get; set; }



        public SalesPromotion()
        {
            AppliedCommodityIds = new ValueArray<int>();
            Price = new Money();
        }

        public bool IsContainCommodity(int commodityId)
        {
            if(TargetType == SalesPromotionTargetType.All)
            {
                return true;
            }
            else
            {
                return AppliedCommodityIds.Items.Contains(commodityId);
            }
        }

        public void SetEndTime(int hours, int minutes)
        {
            EndTime = BeginTime.AddHours(hours).AddMinutes(minutes);
        }

        public int Hours
        {
            get
            {
                var temp = EndTime - BeginTime;
                return temp.Hours;
            }
        }

        public int Minutes
        {
            get
            {
                var temp = EndTime - BeginTime;
                return temp.Minutes % 60;
            }
        }

        public bool IsOngoing
        {
            get
            {
                DateTime now = DateTime.Now;
                return now > BeginTime && now < EndTime;
            }
        }
        public bool IsFinished
        {
            get
            {
                return DateTime.Now > EndTime;
            }
        }
        public bool IsNotStarted
        {
            get
            {
                return DateTime.Now < BeginTime;
            }
        }

        public long? CreatorUserId { get; set; }
        public DateTime CreationTime { get; set; }
    }

    public enum SalesPromotionTargetType
    {
        /// <summary>
        /// 部分商品
        /// </summary>
        All = 0,

        /// <summary>
        /// 全部商品
        /// </summary>
        Part = 1
    }
}
